package com.yuan.department.dao.impl;

import com.yuan.department.dao.IDeptDao;
import com.yuan.department.entity.Dept;
import com.yuan.department.utils.DruidPoolUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @program: Train
 * @description:
 * @author: yuan_boss
 * @create: 2022-11-23 17:16
 **/
public class DeptDaoImpl implements IDeptDao {
    QueryRunner qr = new QueryRunner(DruidPoolUtil.dataSource);
    @Override
    public Integer insert(Dept dept) throws SQLException {
        String sql = "insert into t_dept(dname,loc) values(?,?)";
        return qr.update(sql,dept.getDname(),dept.getLoc());
    }

    @Override
    public Dept selectById(Integer id) throws SQLException {
        String sql = "select * from t_dept where deptno = ?";
        Dept query = qr.query(sql, new BeanHandler<>(Dept.class), id);
        return query;
    }

    @Override
    public Integer updateById(Dept dept) throws SQLException {
        String sql = "update t_dept set dname = ?,loc = ? where deptno = ?";
        return qr.update(sql,dept.getDname(),dept.getLoc(),dept.getDeptno());
    }

    @Override
    public Integer deleteById(Integer id) throws SQLException {
        String sql = "delete from t_dept where deptno = ? ";
        return qr.update(sql,id);
    }

    @Override
    public List<Dept> selectList() throws SQLException {
        String sql = "select * from t_dept";
        List<Dept> query = qr.query(sql, new BeanListHandler<>(Dept.class));
        return query;
    }
}
